using System;
using System.Collections.Generic;
using System.Text;
using DBConnectionLib;
using Microsoft.SqlServer.Management;
using System.Collections.Specialized;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

using Fireball.Windows.Forms;
using Fireball.Syntax;
using Fireball.CodeEditor.SyntaxFiles;



namespace DBSourceToolsLib.SysObjects
{
    public class SOUser : SysObject
    {
        public SOUser(string strName, string strBaseDirectory, Database oDatabase, string strUrn)
            : base(strName, strBaseDirectory, oDatabase, SysObjectType.User)
        {
            this.Urn = strUrn;
            //LoadScript(oDatabase);
        }
        public SOUser(string strFileName, Database oDatabase)
            : base(SysObjectType.User, strFileName, oDatabase)
        {

        }

        public SOUser(SysObject oldSysObject, Database targetDatabase)
            :base ( oldSysObject.Name, targetDatabase.BaseDirectory, targetDatabase, SysObjectType.User)
        {
            this.Text = oldSysObject.Text;
            this.Text = this.Text.Replace("[" + oldSysObject.DBName + "]", "[" + targetDatabase.DatabaseName + "]");
        }

        public override void LoadScript(Database oDatabase, string strOutputFileName)
        {
        }

        #region IDisplayPanel Members

        public override System.Windows.Forms.Panel GetPanel()
        {
            Panel oPanel = new Panel();
            oPanel.Dock = DockStyle.Fill;

            CodeEditorControl codeEdit = new CodeEditorControl();
            codeEdit.Dock = DockStyle.Fill;

            CodeEditorSyntaxLoader.SetSyntax(codeEdit, SyntaxLanguage.SqlServer2K);
            codeEdit.Document.Text = this.Text;
            oPanel.Controls.Add(codeEdit);
            return oPanel;
        }

        #endregion
        internal void LoadScript(Database oDatabase, string strOutputFileName, Microsoft.SqlServer.Management.Smo.User oSmo)
        {
            base.EnsureDirectoryExists(strOutputFileName);

            Microsoft.SqlServer.Management.Smo.ScriptingOptions oScriptingOptions = new Microsoft.SqlServer.Management.Smo.ScriptingOptions();
            oScriptingOptions.ClusteredIndexes = true;
            oScriptingOptions.NonClusteredIndexes = true;
            oScriptingOptions.DriAll = true;
            oScriptingOptions.Indexes = true;
            oScriptingOptions.IncludeDatabaseContext = false;
            oScriptingOptions.IncludeDatabaseRoleMemberships = true;
            //oScriptingOptions.ExtendedProperties = true;
            oScriptingOptions.FileName = strOutputFileName;
            oScriptingOptions.ExtendedProperties = UserSettings.Instance().UseExtendedProperties;
            oScriptingOptions.AnsiFile = UserSettings.Instance().UseANSII;


            StringBuilder oStringBuilder = new StringBuilder();
            StringCollection myStrColl = new StringCollection();
            myStrColl = oSmo.Script(oScriptingOptions);

            base.SetTextFromFile(strOutputFileName);

        }

        public override DBSourceToolsLib.Composite.DBComposite GetDBComposite()
        {
            //return new Composite.DBComposite(this.QualifiedName);
            return new Composite.DBComposite(this.Name, this.NameNoSchema, 
                Composite.DBCompositeType.User,
                new Composite.TreeNodeTag(this.m_Database.NickName,
                "File", this.FileName, this.QualifiedName));

        }

    }
}
